package com.tjhk.java.tech.thread;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class CacheThreadPool2 {
    public static Integer extractResult(Future<Integer> f){
        try {
            return f.get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ExecutorService exec = Executors.newCachedThreadPool();
        List<CountingTask2> tasks
                = IntStream.range(0,30).mapToObj(CountingTask2::new).collect(Collectors.toList());
        List<Future> futures = tasks.stream().map(s->exec.submit(s)).collect(Collectors.toList());
//        Integer sum = futures.stream().map(CacheThreadPool2::extractResult)
//                .reduce(0,Integer::sum);
//        System.out.println(sum);
        exec.shutdown();
    }
}
